<?php 
include_once('connection/connection.php');
include('config.php');
//session_start();
//ob_start();
include('header.php');
include('menu.php');
$formId=trim($_REQUEST['formId']);
//$formId=$_SESSION['formID'];
$sqlGetFormDetails="select * from tbl_dtls_q1_main where id=$formId";
$queryFormDetails=mysql_query($sqlGetFormDetails);
$rowFormDetails=mysql_fetch_array($queryFormDetails);
?>
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<link href="css/style_question.css" type="text/css" rel="stylesheet" />
<script language="javascript" type="text/javascript" src="js/validate1.js"></script>
<script language=javascript>
function show_critical(val,i)
{ 
		if(val == "yes" )
		{
			document.getElementById('crit'+i).style.display = "block";	
		}
		else
		{
			document.getElementById('crit'+i).style.display = "none";
			
		/////////////////////////////////////////////////////////////////////		
		var a=document.getElementsByName('CH_quality'+i+'[]')
		a[0].checked=false;
		a[1].checked=false;
		a[2].checked=false;
		a[3].checked=false;
		a[4].checked=false;
		var b=document.getElementsByName('CH_quantity'+i+'[]')
		b[0].checked=false;
		b[1].checked=false;
		b[2].checked=false;
		b[3].checked=false;
		b[4].checked=false;
		///////////////////////////////////////////////////////////////////////
		} 
}
function show_tanker(val,i)
{
//alert(val);
		if(val == "yes" )
		{
			document.getElementById('tanker').style.display = "block";	
		}
		else
		{
			var num = document.getElementById('habcount').value;
			document.getElementById('tanker').style.display = "none";
			for(i=1; i<=num; i++)
			{
			document.getElementById('water_supply_no'+i).value="";
			document.getElementById('water_supply_qty'+i).value="";
			document.getElementById('water_supply_area'+i).value="";
			}
		}
}
function onload_critical()
{ 
		var counthab=document.getElementById('habcount').value;
		for(var i=1; i<counthab; i++)
		{
		var arr=document.getElementsByName('critical'+i);
		if(arr[0].checked)
		{
			document.getElementById('crit'+i).style.display = "block";	
		}
		else
		{
			document.getElementById('crit'+i).style.display = "none";
		}
		} 
}
</script>
<title>Questionnaire No.1</title>
</head>
<body onLoad="onload_critical();">
<?php

if(isset($_POST['submit']))
{
	$fclick = true;
	$loc_id=$formId;
	//$loc_id = $_SESSION['loc_id'];
	//$qr1="SELECT DISTINCT  habit_name, habit_code FROM tbl_dtls_q1_oh WHERE status_e_nt='Existing' AND loc_id=(select max(loc_id) FROM tbl_dtls_q1_oh)";
	//echo $qr1;
	$qr1="SELECT DISTINCT  habit_name, habit_code FROM tbl_dtls_q1_oh WHERE status_e_nt='Existing' AND loc_id='$loc_id'";
	//echo $qr1;
	$rec1=mysql_query($qr1);
	$rows_num = mysql_num_rows($rec1);
	$_SESSION['rows_num']=$rows_num;
	for($i=1; $i<=$rows_num; $i++)
	{
		$critical_hname = 'critical_hname'.$i;
		$critical_hcode = 'critical_hcode'.$i;
		$type1_habit = 'type1_habit'.$i;
		$critical = 'critical'.$i;
		$CH_quality='CH_quality'.$i;
		$CH_quantity='CH_quantity'.$i;
		
		$critical_hname=$_POST[$critical_hname];
		$critical_hcode=$_POST[$critical_hcode];
		$type1_habit=$_POST[$type1_habit];
		//echo 'type' . $type1_habit;
		$critical=$_POST[$critical];
		$CH_quality=$_POST[$CH_quality];
		if(count($CH_quality)!=0)
		{	
			$CH_quality=implode(',',$CH_quality);
		}
		else
		{
			$CH_quality='';
		}
		
		$CH_quantity=$_POST[$CH_quantity];	
		if(count($CH_quantity)!=0)
		{	
			$CH_quantity=implode(',',$CH_quantity);
		}
		else
		{
			$CH_quantity='';
		}
		
		//$qr2="INSERT INTO tbl_dtls_q1_critical(loc_id, critical_hname, critical_hcode, type_habit,  critical, CH_quality, CH_quantity) VALUES ('$loc_id', '$critical_hname', '$critical_hcode', '$type1_habit', '$critical', '$CH_quality', '$CH_quantity')";
		//echo $qr2="UPDATE tbl_dtls_q1_critical SET critical_hname='$critical_hname', critical_hcode='$critical_hcode', type_habit='$type1_habit', critical='$critical', CH_quality='$CH_quality', CH_quantity='$CH_quantity' WHERE loc_id=$formId";
		$qr2="UPDATE tbl_dtls_q1_critical SET critical='$critical', CH_quality='$CH_quality', CH_quantity='$CH_quantity' WHERE loc_id=$formId and critical_hcode=$critical_hcode";		
		//echo $qr2;
		$rec2=mysql_query($qr2);
		
	
		$water_supply_hcode = 'water_supply_hcode'.$i;
		$water_supply_hname = 'water_supply_hname'.$i;
		$type2_habit = 'type2_habit'.$i;
		$water_supply_no = 'water_supply_no'.$i;
		$water_supply_qty = 'water_supply_qty'.$i;
		$water_supply_area = 'water_supply_area'.$i;
		$water_supply_hcode=$_POST[$water_supply_hcode];	
		$water_supply_hname=$_POST[$water_supply_hname];
		$type2_habit=$_POST[$type2_habit];
		$water_supply_no=$_POST[$water_supply_no];
		$water_supply_qty=$_POST[$water_supply_qty];
		$water_supply_area=$_POST[$water_supply_area];
		
		$arr_habit_code[$i]["code"]=$water_supply_hcode;
		$arr_habit_name[$i]["name"]=$water_supply_hname;
		$_SESSION['arr_habit_code']=$arr_habit_code;
		$_SESSION['arr_habit_name']=$arr_habit_name;

		//$qr3="INSERT INTO tbl_dtls_q1_tanker (loc_id,habitation_code, habitation_name, type_habit, total_tanker_no, water_quantity, area_covered) value ('$loc_id', '$water_supply_hcode', '$water_supply_hname', '$type2_habit', '$water_supply_no', '$water_supply_qty', '$water_supply_area')";
		//echo $qr3="UPDATE tbl_dtls_q1_tanker SET habitation_name='$water_supply_hname', habitation_code='$water_supply_hcode', type_habit='$type2_habit', total_tanker_no='$water_supply_no', water_quantity='$water_supply_qty', area_covered='$water_supply_area' WHERE loc_id=$formId";
		$qr3="UPDATE tbl_dtls_q1_tanker SET total_tanker_no='$water_supply_no', water_quantity='$water_supply_qty', area_covered='$water_supply_area' WHERE loc_id=$formId and habitation_code=$water_supply_hcode";
		//echo $qr3;
		$rec3=mysql_query($qr3);
		
	}
			if($rec2)
			{
				//header('Location: questionnaire3_2.php');
				//echo "success 1";	
				$relocatePage=true;	
			}
			else 
			{
				$errorIns="Error Updating tbl_dtls_q1_critical table <br>";
			}
			if($rec3)
			{
				//header('Location: questionnaire3_2.php');
				//echo "success 1";	
				$relocatePage=true;	
			}
			else 
			{
				$errorIns="Error Updating tbl_dtls_q1_tanker table <br>";
			}
	/*$qr10="UPDATE tbl_dtls_q1_main SET status='1' WHERE id='$loc_id'";	
	$result10=mysql_query($qr10);*/
	
	 $q="SELECT usertype FROM tbl_dtls_createuser WHERE loginid='$loginid'";
			$res=mysql_query($q);
			$x=mysql_fetch_array($res);
			$user_type=$x['usertype'];
			$user_id=$_SESSION['loginid'];
			$form_id=$_SESSION['form_id'];
			$user_name=$_SESSION['name'];
			//$user_type=$_SESSION['usertype'];
			$ip=$_SERVER['REMOTE_ADDR'];
			$qr1="INSERT INTO tbl_dtls_edit_history(user_id, form_id, action_id, ip_address, modify_date, user_type, user_name) VALUES ('$user_id', '$form_id', '4', '$ip', NOW(), '$user_type', '$user_name')";
			//echo $q5;
			$result=mysql_query($qr1);	
			if($result)
			{
				//header('Location: questionnaire3_2.php');
				//echo "success 1";	
				$relocatePage=true;	
			}
			else 
			{
				$errorIns="Error inserting tbl_dtls_edit_history table <br>";
			}	
			if($errorIns==0) 
			{
				echo'<span class="successmssg"><b>Congratulation!!</b><br/>You have successfully updated this Page. Please go to <a href="dd_questionnaire_page.php">Edit Questionnaire Page Wise</a></span>';
				exit();
			}
			else 
			{
				echo $errorIns;exit;
			}	
		/*if($rec2 && $rec3 && result)
		{
			//echo'<span class="successmssg"><b>Congratulation!!</b><br/>You have successfully filled this Form. Now you can go on <a href="home.php">home</a> page or fill the <a href = "questionnaire1.php">Questionnaire 1</a> form again.</span>';
			echo'<span class="successmssg"><b>Congratulation!!</b><br/>You have successfully filled this Form.</span>';
			exit();
			//echo 'success1';
		}*/
}
else
{
	$fclick = false;
	$flag=0;    
}
?>
<div align="center">
<form name="f1" action="" method="post" onSubmit="return validate4()">
	<table border="0" width="750" id="table1" cellspacing="0" cellpadding="0">
		<tr>
			<td height="30" colspan="3">
			<p align="center">
			<font face="Verdana" style="font-size: 10pt; font-weight: 700" color="#000000">
			Questionnaire No. 1</font></td>
		</tr>		
		<tr>
			<td colspan="3" align="center"><font face="Arial" style="font-size: 8.5pt" color="#000000">(For PHED Officials)</font></td>
		</tr>
		<tr>
			<td colspan="3"align="center"><font face="Arial" style="font-size: 8.5pt" color="#000000">page 4</font></td>
		</tr>
		<tr>
			<td colspan="3">&nbsp;</td>
		</tr>
		<tr>
			<td height="22" colspan="2" bgcolor="#EEEEEE"><font color="#000000">&nbsp;
			<span style="font-weight: 700">
			<font face="Arial" style="font-size: 8.5pt">Critical Habitations</font></span>
			</font></td>
		</tr>
		<tr>
			<td colspan="2">
				<table border="0" width="750" id="table2" bgcolor="#FDF9EE" cellspacing="1" cellpadding="0">
					<tr>
						<!--<td colspan="5">
							<span style="font-size: 4pt">&nbsp;</span>
						</td>-->
						<td width="204" height="28" align="center"  bgcolor="#F7E6B7">
							<font face="Verdana" style="font-size: 8.5pt" color="#990000">
							Habitation Name</font></td>
						<td width="110" align="center"  bgcolor="#F7E6B7"><font face="Verdana" style="font-size: 8.5pt" color="#990000">Habitation Type</font></td>
						<td width="432"  bgcolor="#F7E6B7" align="center">
							<font face="Verdana" style="font-size: 8.5pt" color="#990000">
							Critical</font></td>
					</tr>
					<?php
					$sql_crit="select * from tbl_dtls_q1_critical where loc_id=$formId order by critical_hcode";
					$query_crit=mysql_query($sql_crit);
					//$row_crit=mysql_fetch_array($query_crit);
					//echo $row_crit['critical_hname'];
 	 			$i=1;
				while($row_crit=mysql_fetch_array($query_crit)){
				//echo $row_crit['critical_hname'];
				?>
				<tr align="center">
					<td width="204">
					<input type="text" name="critical_hname<?php echo $i;?>" id="critical_hname<?php echo $i;?>" value="<?php echo $row_crit['critical_hname'];?>" readonly style="font-size: 8pt; font-family: Verdana; border-style: solid; border-width: 1px">
	                <input type="hidden" name="critical_hcode<?php echo $i;?>" id="critical_hcode<?php echo $i;?>" value="<?php echo $row_crit['critical_hcode'];?>" readonly style="font-size: 8pt; font-family: Verdana; border-style: solid; border-width: 1px"></td>
				    <td align="center" width="110"><input type="text" name="type1_habit<?php echo $i;?>" id="type1_habit<?php echo $i;?>" value="<?php echo $row_crit['type_habit'];?>" size="2" class="txt_1" readonly="readonly"/></td>
				    <td width="432">
				  <font style="font-size: 9pt" face="Verdana" color="#000000">
				  <input type="radio" name="critical<?php echo $i;?>" id="critical<?php echo $i;?>" value="yes"<?php echo($row_crit['critical']=='yes')?'checked':'';?> onClick="show_critical(this.value,<?php echo $i; ?>)">Yes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	  		  	  <input type="radio" name="critical<?php echo $i;?>" id="critical<?php echo $i;?>" value="no"<?php echo($row_crit['critical']=='no')?'checked':'';?> onClick="show_critical(this.value,<?php echo $i; ?>)">No&nbsp;</font></td>
				</tr> 
					<tr>
						<td></td>
						<td></td>
						<td><div id="crit<?php echo $i;?>" class="hideme">
							<table border="0" width="100%" id="table16" cellpadding="0">
							<tr>
								<td height="22" colspan="5">
								<font face="Verdana" style="font-size: 8.5pt" color="#990000">&nbsp; 
								Critical Village in terms of quality </font>							                        		</td>
							</tr>
							<tr>
								<td width="93" height="24">&nbsp;
								<?php
								$quality=explode(',',$row_crit['CH_quality']);
								?>
								<!--<input type="checkbox" name="exist_water_supplied[]" id="exist_water_supplied[]" value="bulk_meters"<?php //echo  (in_array('bulk_meters',$exist_means)?'checked':'');?>>-->
					  	   		<input type="checkbox" name="CH_quality<?php echo $i;?>[]" id="CH_quality<?php echo $i;?>[]" value="Fluoride"<?php echo  (in_array('Fluoride',$quality)?'checked':'');?>>
						  		<font face= "Verdana" style="font-size: 8.5pt" color="#000000">                           		Fluoride </font> </td>
				    		  <td width="87"><input type="checkbox" name="CH_quality<?php echo $i;?>[]" id="CH_quality<?php echo $i;?>[]" value="Nitrates"<?php echo  (in_array('Nitrates',$quality)?'checked':'');?>>
			    		      <font face= "Verdana" style="font-size: 8.5pt" color="#000000"> Nitrates</font> </td>
					    		<td width="61">
				 	    		<input type="checkbox" name="CH_quality<?php echo $i;?>[]" id="CH_quality<?php echo $i;?>[]" value="TDS"<?php echo (in_array('TDS',$quality)?'checked':'');?>><font style="font-size: 8.5pt" face="Verdana" color="#000000">TDS</font> </td>
					    		<td width="98">
						  		<input type="checkbox" name="CH_quality<?php echo $i;?>[]" id="CH_quality<?php echo $i;?>[]" value="Salinity"<?php echo (in_array('Salinity',$quality)?'checked':'');?>> 
					  	  		<font style="font-size: 8.5pt" face="Verdana" color="#000000">Salinity                          		</font></td>
								
						      <td width="81"><input type="checkbox" name="CH_quality<?php echo $i;?>[]" id="CH_quality<?php echo $i;?>[]" value="None"<?php echo (in_array('None',$quality)?'checked':'');?>><font style="font-size: 8.5pt" face="Verdana" color="#000000">None</font></td>
							</tr>
							<tr>
								<td height="26" colspan="5">&nbsp;
								<font face="Verdana" style="font-size: 8.5pt" color="#990000">
								Critical Village in terms of quantity </font>            						                       			</td>
							</tr>
							<tr>
								<td height="26" colspan="2">&nbsp;&nbsp;
								<?php
								$quantity=explode(',',$row_crit['CH_quantity']);
								?>
								<input type="checkbox" name="CH_quantity<?php echo $i;?>[]" id="CH_quantity<?php echo $i;?>[]" value="Non Availability of water"<?php echo (in_array('Non Availability of water',$quantity)?'checked':'');?>><font style="font-size: 8.5pt" face="Verdana" color="#000000">&nbsp;Non Availability of water &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;in Village</font></td>
					    		<td height="26" colspan="3"><input type="checkbox" name="CH_quantity<?php echo $i;?>[]" id="CH_quantity<?php echo $i;?>[]" value="Insufficient quantity of water"<?php echo (in_array('Insufficient quantity of water',$quantity)?'checked':'');?>><font style="font-size: 8.5pt" face="Verdana" color="#000000"> Insufficient quantity of water to meet &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;minimum demand</font></td>
				    		</tr>
							<tr>
								<td height="40" colspan="2">
							  &nbsp;
							  <input type="checkbox" name="CH_quantity<?php echo $i;?>[]2" id="CH_quantity<?php echo $i;?>[]2" value="Emergency response system"<?php echo (in_array('Emergency response system',$quantity)?'checked':'');?>>
							  <font style="font-size: 8.5pt" face="Verdana" color="#000000">&nbsp;Emergency response &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;system</font> </td>
					    		<td height="40" colspan="3"><input type="checkbox" name="CH_quantity<?php echo $i;?>[]" id="CH_quantity<?php echo $i;?>[]" value="Excess drawal of water"<?php echo (in_array('Excess drawal of water',$quantity)?'checked':'');?>><font style="font-size: 8.5pt" face="Verdana" color="#000000"> Excess drawal of water by &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;intermediate village</font></td>
				    		</tr>
							<tr>
								<td height="26" colspan="2">
								&nbsp;&nbsp;<input type="checkbox" name="CH_quantity<?php echo $i;?>[]" id="CH_quantity<?php echo $i;?>[]" value="None"<?php echo (in_array('None',$quantity)?'checked':'');?>><font style="font-size: 8.5pt" face="Verdana" color="#000000">&nbsp;None</font></td>
				    		</tr>
						</table>
						</div></td>
				</tr>
				<?php  $i++;
					} ?>
			</table>		</td>
	</tr>
		<tr>
			<td height="19" colspan="2"><span style="font-size: 4pt">&nbsp;</span></td>
		</tr>
		<tr>
			<td width="207" height="23" bgcolor="#EEEEEE"><font color="#000000">&nbsp;
				<span style="font-weight: 700">
				<font face="Arial" style="font-size: 8.5pt">Water Supply by Tankers</font></span></font></td>
		    <td width="543">
				  <font style="font-size: 9pt" face="Verdana" color="#000000">
				  <input type="radio" name="supply<?php echo $i;?>" id="supply<?php echo $i;?>" value="yes" onClick="show_tanker(this.value)">Yes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  	      <input type="radio" name="supply<?php echo $i;?>" id="supply<?php echo $i;?>" value="no" onClick="show_tanker(this.value)" checked="checked">No&nbsp;</font></td>
		</tr>
		<tr>
			<td height="15" colspan="2"><span style="font-size: 3pt">&nbsp;</span></td>
		</tr>
		<tr>
			<td colspan="2">
				<table border="0" width="100%" id="table6" bgcolor="#FDF9EE" cellspacing="1" cellpadding="0">
					<tr>
						<td height="64">
							<table border="0"  width="100%" id="table8" cellpadding="0" bgcolor="#FDF9EE" cellspacing="1">
								<tr>
								<td><div id="tanker">
                           		 <table width="750">
                               	 <tr>
									<td width="201" height="29" align="center"  bgcolor="#F7E6B7">
										<font face="Verdana" style="font-size: 8.5pt" color="#990000">
										Habitation Name</font></td>
										<td width="113" align="center"  bgcolor="#F7E6B7"><font face="Verdana" style="font-size: 8.5pt" color="#990000">Habitation Type</font></td>
									<td width="120"  bgcolor="#F7E6B7" align="center">
										<address>
										<span style="font-style: normal">
										<font face="Verdana" style="font-size: 8.5pt" color="#990000">
										Tanker Nos.</font></span>
										</address>								  </td>
									<td width="139"  bgcolor="#F7E6B7" align="center">
										<address><span style="font-style: normal">
										<font face="Verdana" style="font-size: 8.5pt" color="#990000">
										Quantity (KL)</font></span></address>								  </td>
									<td width="169"  bgcolor="#F7E6B7" align="center">
										<address><span style="font-style: normal">
										<font face="Verdana" style="font-size: 8.5pt" color="#990000">
										Area Covered</font></span></address></td>
								</tr>
								
				<?php
				$sql_tanker="select * from tbl_dtls_q1_tanker where loc_id=$formId order by habitation_code";
				$query_tanker=mysql_query($sql_tanker);
				//$row_tanker=mysql_fetch_array($query_tanker);
 	 			$i=1;
				while($row_tanker=mysql_fetch_array($query_tanker)){
				?>
								<tr align="center">
									<td width="201" height="30">
									<input type="text" name="water_supply_hname<?php echo $i;?>" id="water_supply_hname<?php echo $i;?>" value="<?php echo $row_tanker['habitation_name'];?>" readonly style="font-size: 8pt; font-family: Verdana; border-style: solid; border-width: 1px">
	                                 <input type="hidden" name="water_supply_hcode<?php echo $i;?>" id="water_supply_hcode<?php echo $i;?>" value="<?php echo $row_tanker['habitation_code'];?>" readonly style="font-size: 8pt; font-family: Verdana; border-style: solid; border-width: 1px">								  </td>
								  <td align="center" width="113"><input type="text" name="type2_habit<?php echo $i;?>" id="type2_habit<?php echo $i;?>" value="<?php echo $row_tanker['type_habit'];?>" size="2" class="txt_1" readonly="readonly"/></td>
									<td width="120">
									<input type="text" name="water_supply_no<?php echo $i;?>" id="water_supply_no<?php echo $i;?>" value="<?php echo $row_tanker['total_tanker_no'];?>" class="txt5" onBlur="numeric(this)">								  </td>
									<td width="139">
									<input type="text" name="water_supply_qty<?php echo $i;?>" id="water_supply_qty<?php echo $i;?>" value="<?php echo $row_tanker['water_quantity'];?>" class="txt5" onBlur="numeric(this)">								  </td>
									<td width="169">
									<input type="text" name="water_supply_area<?php echo $i;?>" id=												"water_supply_area<?php echo $i;?>"  value="<?php echo $row_tanker['area_covered'];?>" class="txt5">								  </td>
								</tr>
								<?php  $i++;
								} ?>
								<input type="hidden" name="habcount" id="habcount" value="<?php echo $i; ?>">
								</table>
								</div></td></tr>
						</table>
					</td>
				</tr>
			</table>		</td>
	</tr>
	<tr>
		<td colspan="2">&nbsp;</td>
	</tr>
	<tr>
		<td colspan="2">
			<table border="0" width="100%" id="table4" cellspacing="1" cellpadding="0" bgcolor="#FDF9EE">
			</table>		</td>
	</tr>
	<tr>
		<td colspan="2">&nbsp;</td>
	</tr>
	<tr>
		<td colspan="2">&nbsp;</td>
	</tr>
	<tr>
		<td colspan="2">
			<p align="center">
			<input type="submit" value="Update" name="submit" style="font-size: 8pt;font-family: Verdana; border-style: solid; border-width: 1px">
		</td>
	</tr>
</table>
</form>
</div>
</body>
</html>